#include "messagepickerstandarditemmodel.h"

MessagePickerStandardItemModel::MessagePickerStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    header<<"日期"
         <<"单号"
        <<"客户名称"
       <<"联系人"
      <<"电话"
    <<"金额";

    setHorizontalHeaderLabels(header);
}

void MessagePickerStandardItemModel::reset(){
    clear();
    messages.clear();

    setHorizontalHeaderLabels(header);
}

void MessagePickerStandardItemModel::setWidgetType(WidgetType widgetType){
    this->widgetType = widgetType;
}

void MessagePickerStandardItemModel::insertMessage(Message *message){
    QList<QStandardItem*> rowData;

    UserRelation *relation = message->getUserRelation();

    rowData.append(new QStandardItem(message==nullptr?"":message->date));
    rowData.append(new QStandardItem(message==nullptr?"":message->messageId));
    rowData.append(new QStandardItem(relation==nullptr?"":relation->nickName));
    rowData.append(new QStandardItem(relation==nullptr?"":relation->contact));
    rowData.append(new QStandardItem(relation==nullptr?"":relation->tel));
    switch (widgetType) {
    case WidgetType::TicketAdjust:
        rowData.append(new QStandardItem(message==nullptr?"0":QString::number(message->getMoneyValue(), 'f', 2)));
        break;
    case WidgetType::TicketIn:
        rowData.append(new QStandardItem(message==nullptr?"0":QString::number(message->ticketOut, 'f', 2)));
        break;
    case WidgetType::MoneyIn:
        rowData.append(new QStandardItem(message==nullptr?"0":QString::number(message->moneyOut, 'f', 2)));
        break;
    }

    appendRow(rowData);

    messages.append(message);
}

void MessagePickerStandardItemModel::insertMessage(QList<Message*> messages){
    for(Message *message:messages){
        insertMessage(message);
    }
}

Message* MessagePickerStandardItemModel::getMessage(int index){
    if(index<0 || index>=messages.size()){
        return nullptr;
    }

    return messages[index];
}
